package com.kingdee.eas.custom.hostelmgr.quickmove.app;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;

import com.kingdee.bos.BOSException;
import com.kingdee.bos.Context;
import com.kingdee.bos.dao.ormapping.ObjectStringPK;
import com.kingdee.bos.dao.ormapping.ObjectUuidPK;
import com.kingdee.bos.metadata.entity.EntityViewInfo;
import com.kingdee.bos.metadata.entity.FilterInfo;
import com.kingdee.bos.metadata.entity.FilterItemInfo;
import com.kingdee.bos.metadata.query.util.CompareType;
import com.kingdee.eas.basedata.person.PersonFactory;
import com.kingdee.eas.basedata.person.PersonInfo;
import com.kingdee.eas.common.EASBizException;
import com.kingdee.eas.custom.hostelmgr.basedata.DistributionOrNotEnum;
import com.kingdee.eas.custom.hostelmgr.basedata.HostelBasicFactory;
import com.kingdee.eas.custom.hostelmgr.basedata.HostelBasicInfo;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionCollection;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionFactory;
import com.kingdee.eas.custom.hostelmgr.beddistribution.BedDistributionInfo;
import com.kingdee.eas.framework.CoreBaseCollection;
import com.kingdee.eas.util.app.DbUtil;

public class QuickMoveFacadeControllerBean extends
		AbstractQuickMoveFacadeControllerBean {
	private static Logger logger = Logger
			.getLogger("com.kingdee.eas.custom.hostelmgr.quickmove.app.QuickMoveFacadeControllerBean");

	public void match(Context ctx,
			BedDistributionCollection distributionCollection)
			throws BOSException, EASBizException {
		super.match(ctx, distributionCollection);
		CoreBaseCollection personCollection = new CoreBaseCollection();

		CoreBaseCollection distCollection = new CoreBaseCollection();
		BedDistributionInfo distribution = null;
		HostelBasicInfo exisitBed = null;
		String sql = "UPDATE T_HR_PERSONCONTACTMETHOD SET FADDRESS=? WHERE FPERSONID=?";
		String distributionSql = "UPDATE CT_HOS_BEDDISTRIBUTION SET CFPERSONID=? WHERE CFTREEIDID=?";
		String personSql = "UPDATE T_BD_PERSON SET CFBEDID=?,CFISDISTRIBUTION=? WHERE FID=?";

		for (int i = 0; i < distributionCollection.size(); i++) {
			distribution = distributionCollection.get(i);

			PersonInfo person = distribution.getPerson();
			HostelBasicInfo bed = distribution.getBed();

			if (!this.checkPersonExisit(ctx, person.getId().toString())) {
				continue;
			}

			if ((exisitBed = this.checkBedExisit(ctx, bed.getId().toString())) == null) {
				continue;
			}
			distCollection.add(distribution);

			BedDistributionInfo bedDistInfo = this.checkBedIsDistribution(ctx,
					bed.getId().toString());

			if (bedDistInfo != null && bedDistInfo.getPerson() != null) {
				DbUtil.execute(ctx, sql, new Object[] { null,
						bedDistInfo.getPerson().getId().toString() });
			}

			BedDistributionInfo personDistInfo = this
					.checkPersonIsDistribution(ctx, person.getId().toString());

			if (personDistInfo != null && personDistInfo.getBed() != null) {
				DbUtil.execute(ctx, distributionSql, new Object[] { null,
						personDistInfo.getBed().getId().toString() });
			}

			DbUtil.execute(ctx, sql, new Object[] { exisitBed.getDisplayName(),
					person.getId().toString() });

			DbUtil.execute(ctx, distributionSql, new Object[] {
					person.getId().toString(), bed.getId().toString() });

			person.setIsDistribution(DistributionOrNotEnum.YES);
			person.setBed(bed);

			DbUtil.execute(ctx, personSql, new Object[] {
					bed.getId().toString(), DistributionOrNotEnum.YES_VALUE,
					person.getId().toString() });
		}
	}

	public void unMatch(Context ctx,
			BedDistributionCollection distributionCollection)
			throws BOSException, EASBizException {
		super.unMatch(ctx, distributionCollection);
		CoreBaseCollection personCollection = new CoreBaseCollection();
		CoreBaseCollection collection = new CoreBaseCollection();
		BedDistributionInfo distribution = null;
		HostelBasicInfo exisitBed = null;
		String sql = "UPDATE T_HR_PERSONCONTACTMETHOD SET FADDRESS=? WHERE FPERSONID=?";
		List<Object[]> list = new ArrayList<Object[]>();

		for (int i = 0; i < distributionCollection.size(); i++) {
			distribution = distributionCollection.get(i);

			PersonInfo person = distribution.getPerson();
			HostelBasicInfo bed = distribution.getBed();

			if (!this.checkPersonExisit(ctx, person.getId().toString())) {
				BedDistributionInfo bedDistributionInfo = this
						.checkPersonIsDistribution(ctx, person.getId()
								.toString());
				if (bedDistributionInfo != null) {
					bedDistributionInfo.setPerson(null);
					collection.add(bedDistributionInfo);
				}
				continue;
			}

			if ((exisitBed = this.checkBedExisit(ctx, bed.getId().toString())) == null) {
				continue;
			}

			BedDistributionInfo bedDistributionInfo = this
					.checkPersonIsDistribution(ctx, person.getId().toString());
			if (bedDistributionInfo != null
					&& bedDistributionInfo.getBed().getId().equals(bed.getId())) {
				person.setBed(null);
				person.setIsDistribution(DistributionOrNotEnum.NO);
				personCollection.add(person);
				distribution.setPerson(null);
				collection.add(distribution);
				Object[] param = new Object[] { "", person.getId().toString() };
				list.add(param);
			}

		}
		BedDistributionFactory.getLocalInstance(ctx)
				.updateBatchData(collection);
		PersonFactory.getLocalInstance(ctx).updateBatchData(personCollection);
	}

	private HostelBasicInfo checkBedExisit(Context ctx, String id)
			throws EASBizException, BOSException {
		return HostelBasicFactory.getLocalInstance(ctx).getHostelBasicInfo(
				new ObjectStringPK(id));
	}

	private boolean checkPersonExisit(Context ctx, String id)
			throws EASBizException, BOSException {
		return PersonFactory.getLocalInstance(ctx).exists(
				new ObjectStringPK(id));
	}

	private BedDistributionInfo checkPersonIsDistribution(Context ctx,
			String personId) throws BOSException {
		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("person.id", personId, CompareType.EQUALS));
		view.setFilter(filter);

		BedDistributionCollection collection = BedDistributionFactory
				.getLocalInstance(ctx).getBedDistributionCollection(view);
		if (collection.size() > 0) {
			return collection.get(0);
		} else {
			return null;
		}
		// return
	}

	private BedDistributionInfo checkBedIsDistribution(Context ctx, String bed)
			throws BOSException {

		EntityViewInfo view = new EntityViewInfo();
		FilterInfo filter = new FilterInfo();
		filter.getFilterItems().add(
				new FilterItemInfo("bed.id", bed, CompareType.EQUALS));
		view.setFilter(filter);

		BedDistributionCollection collection = BedDistributionFactory
				.getLocalInstance(ctx).getBedDistributionCollection(view);
		if (collection.size() > 0) {
			return collection.get(0);
		} else {
			return null;
		}
	}
}